import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_tab_search/lf_member/lf_member_view.dart';
import 'package:largefriends/pages/lf_tab_spark/lf_search_filter/lf_search_filter_logic.dart';
import 'package:largefriends/res/lf_text_style.dart';

import '../../res.dart';
import '../../res/lf_color.dart';
import '../../res/lf_fonts.dart';
import 'lf_search/lf_search_view.dart';
import 'lf_tab_search_logic.dart';

class LfTabSearchPage extends StatefulWidget {
  const LfTabSearchPage({super.key});

  @override
  State<LfTabSearchPage> createState() => _LfTabSearchPageState();
}

class _LfTabSearchPageState extends State<LfTabSearchPage>
    with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {
  final logic = Get.put(LfTabSearchLogic());
  List<Widget> pages = List.empty(growable: true);
  RxInt currentIndex = 0.obs;
  late TabController controller;
  @override
  void initState() {
    pages.clear();
    pages.add(const LfSearchPage());
    pages.add(const LfMemberPage());
    controller = TabController(length: 2, vsync: this)
      ..addListener(() {
        currentIndex.value = controller.index;
      });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
        appBar: AppBar(
          automaticallyImplyLeading: false,
          title: ShaderMask(
              shaderCallback: (Rect bounds) {
                return LinearGradient(
                  begin: const Alignment(-0.5, -0.9),
                  end: const Alignment(0.5, 0.9),
                  colors: [colorFF007F, colorFF007F, colorBF00FF, colorBF00FF],
                ).createShader(Offset.zero & bounds.size);
              },
              blendMode: BlendMode.srcATop,
              child: Text(
                'Discover',
                style: TextStyle(
                    fontSize: 28.sp,
                    fontFamily: fontYoungSerif,
                    letterSpacing: 1.2),
              )),
          actions: [
            ShaderMask(
              shaderCallback: (Rect bounds) {
                return LinearGradient(
                  begin: const Alignment(-0.5, -0.9),
                  end: const Alignment(0.5, 0.9),
                  colors: [colorFF007F, colorFF007F, colorBF00FF, colorBF00FF],
                ).createShader(Offset.zero & bounds.size);
              },
              blendMode: BlendMode.srcATop,
              child: InkWell(
                child: Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(horizontal: 10.w),
                  child: Icon(
                    Icons.filter_alt_sharp,
                    color: color210215,
                  ),
                ),
                onTap: () {
                  toLfSearchFilter();
                },
              ),
            )
          ],
        ),
        body: DefaultTabController(
            length: pages.length,
            child: Scaffold(
              appBar: TabBar(
                isScrollable: true,
                tabs: [
                  Obx(() => _tabWidget(
                      title: 'Search', isSelected: currentIndex.value == 0)),
                  Obx(() => _tabWidget(
                      title: 'Verified Members',
                      isSelected: currentIndex.value == 1)),
                ],
                indicator: null,
                indicatorColor: Colors.transparent,
                indicatorPadding: EdgeInsets.zero,
                indicatorSize: TabBarIndicatorSize.tab,
                indicatorWeight: 2.h,
                controller: controller,
              ),
              body: TabBarView(
                controller: controller,
                children: pages,
              ),
            )));
  }

  Widget _tabWidget({required String title, required bool isSelected}) {
    return Text(title,
        style: lfTextStyle(
            sp: 16.sp,
            fontFamily: fontMedium,
            color: isSelected ? colorFF007F : color514F62 ));
  }

  @override
  bool get wantKeepAlive => true;
}
